package cn.ym.mall.service.impl;

import cn.ym.mall.dao.StaffRoleDao;
import cn.ym.mall.dao.impl.StaffRoleDaoImpl;

import cn.ym.mall.entity.StaffRole;
import cn.ym.mall.service.StaffRoleService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StaffRoleServiceImpl implements StaffRoleService {
    StaffRoleDao staffRoleDao = new StaffRoleDaoImpl();
    /**
     * 获得所有员工的角色编号
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<StaffRole> getAll() throws SQLException {
        Connection conn = null;
        List<StaffRole> staffRoleList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            staffRoleList = staffRoleDao.getAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staffRoleList;
    }

    /**
     * 添加新员工角色编号
     *
     * @param staffRole
     * @return
     * @throws SQLException
     */
    @Override
    public boolean add(StaffRole staffRole) throws SQLException {
        Connection conn = null;
        List<StaffRole> staffRoleList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection(false);
            staffRoleList = staffRoleDao.getAll();
            if (staffRoleDao.add(staffRole) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 更新员工角色编号
     *
     * @param staffRole
     * @return
     * @throws SQLException
     */
    @Override
    public boolean update(StaffRole staffRole) throws SQLException {
        Connection conn = null;
        List<StaffRole> staffRoleList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection(false);
            staffRoleList = staffRoleDao.getAll();
            if (staffRoleDao.update(staffRole) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据员工编号，移除角色编号(根据员工编号取消员工权限)
     *
     * @param stid
     * @return
     */
    @Override
    public boolean delete(Integer stid) throws SQLException {
        Connection conn = null;
        List<StaffRole> staffRoleList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection(false);
            staffRoleList = staffRoleDao.getAll();
            if (staffRoleDao.delete(stid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据角色编号查询员工编号(某个角色的所有员工的编号)
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public List<StaffRole> getAllByRid(Integer rid) throws SQLException {
        Connection conn = null;
        List<StaffRole> staffRoleList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            staffRoleList = staffRoleDao.getAllByRid(rid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staffRoleList;
    }
}
